<?
/**
 * 新闻分类
 *
 */
class Newscat extends MyBase{
	private $categorys = array();
	
	function __construct() {
		parent::__construct('news_category.id', array('keyword' => 'name'));
	}
	
	/**
	 * 添加新闻分类
	 */
	public function add($post){
		$rs = $this->db->get_row($this->tbname, array('pid' => $post['pid'], 'name' => $post['name']), 'id');
		if(!empty($rs)){return - 1;}
		
		// 新增
		$data = array(
			'pid'		=> $post['pid'],
			'name'		=> $post['name'],
			'mark'		=> $post['mark'],
			'rank'		=> $post['rank'],
			'memo'		=> $post['memo'],
			'addtime'	=> TIME
		);
		$r = $this->db->insert($this->tbname, $data);
		if($r > 0){
			if($post['pid'] > 0){
				$this->db->update($this->tbname, array('id' => $post['pid']), array('hsub' => 1));
			}
			return $this->cache_news_category();
		}
		return $r;
	}
	
	/**
	 * 修改新闻分类
	 */
	public function edit($post){
		$id = empty($post['id']) ? 0 : intval($post['id']);
		$select = array('pid' => $post['pid'], 'mark' => $post['mark']);
		if($id > 0){
			$select['kept'] = " AND id!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){
			return $this->rwarn(lang('already') . lang('exist'));
		}
		if($id > 0){
			$row = $this->get_row($id, 'pid');
			if(empty($row)){
				$row = array('pid' => -1);
			}
		}
		$data = array(
			'pid'		=> $post['pid'],
			'name'		=> $post['name'],
			'mark'		=> $post['mark'],
			'rank'		=> $post['rank'], 
			'memo'		=> $post['memo'], 
		);
		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			if($row['pid'] != $post['pid']){
				if($post['pid'] > 0){
					$this->db->update($this->tbname, array('id' => $post['pid']), array('hsub' => 1));
				}
				if($row['pid'] > 0){
					$count = $this->db->get_counts($this->tbname, array('pid' => $row['pid']));
					if($count <= 0){
						$this->db->update($this->tbname, array('pid' => $row['pid']), array('hsub' => 0));
					}
				}
			}
			return $this->cache_news_category();
		}
		return $r;
	}
	
	/**
	 * 更新排序
	 */
	public function rank($post){
		if(!is_array($post['rank']))return false;
		$iRtn = 0;
		foreach($post['rank'] as $id => $rank){
			$data = array(
				'rank'		 => $rank,
				'updatetime' => TIME,
			);
			if($this->db->update($this->tbname, array('id' => $id), $data)){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			return $this->cache_news_category();
		}
		return $iRtn;
	}

	/**
	 * 删除新闻分类
	 */
	public function del($post){
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		$iRtn = 0;
		$pid_arr = array();
		foreach($post['list'] as $id){
			$row = $this->get_row($id, 'pid,hsub');
			if(empty($row) || $row['hsub'] == 1){	// 如果有子分类，则跳过
				continue;
			}
			if($this->db->delete($this->tbname, array('id' => $id))){
				$iRtn ++;
				if($row['pid'] > 0)$pid_arr[$row['pid']] = $row['pid'];
			}
		}
		if($iRtn > 0){
			if(!empty($pid_arr)){
				foreach($pid_arr as $pid){
					$count = $this->db->get_counts($this->tbname, array('pid' => $pid));
					if($count <= 0)$this->db->update($this->tbname, array('pid' => $pid), array('hsub' => 0));
				}
			}
			cache_delete('news_child_category.php');
			return $this->cache_news_category();
		}
		return $iRtn;
	}
	
	/**
	 * 取一条记录
	 */
	/*public function get_row($id, $fields = '*'){
		return $this->db->get_row($this->tbname, array('id' => $id), $fields);
	}*/
	
	/**
	 * 取列表
	 */
	/*public function get_list($fields = '*', $select = array(), $order = array(), $page = 1, $size = 10, $offset = PAGE_OFFSET){
		if(!empty($select['keyword'])){
			$select['kept'] = " AND `name` LIKE '%{$select['keyword']}%'";
		}
		return $this->db->get_list($this->tbname, $fields, $select, $order, $page, $size, $offset);
	}*/
	
	/********************************************************************************
	 * 取菜单树
	 ********************************************************************************/
	
	public function get_category_list($pid = 0, $sub = true, $level = 0, $path = '', $depth = 0){
		$list = $this->db->get_all($this->tbname, '*', array('pid' => $pid), array('rank' => 'DESC'));
		if(isset($list['list']) && !empty($list['list'])){
			foreach($list['list'] as $row){
				$row['level'] = $level;
				$row['path'] = $path == '' ? $row['mark'] : $path.'/'.$row['mark'];
				$this->categorys[$row['id']] = $row;
				$next_level = $level + 1;
				if($sub == true && $row['hsub'] == 1 && ($depth == 0 || $next_level < $depth)){
					$this->get_category_list($row['id'], $sub, $next_level, $row['path'], $depth);
				}
			}
		}
		return $this->categorys;
	}

	// 仅子类（子类的子类）
	public function get_category_list_children(&$arr = array(), $pid = 0){
		$ids = $this->db->sql_cols("SELECT `id` FROM `{$this->tbname}` WHERE `pid`={$pid} ORDER BY `rank` DESC,`id` ASC", 'id');
		if(empty($ids)){
			$arr[] = $pid;
		}else{
			foreach($ids as $catid){
				$this->get_category_list_children($arr, $catid);
			}
		}
		return $arr;
	}
	
	public function get_category_tree_ul($pid = 0, $sub = true, $except_id = 0, $level = 99){
		global $__l;	// 语言包
		$categorys = array();
		if($pid == 0){
			$categorys[] = '<li><a href="#" onClick="set_category_id(this, 0, 1);return false;">'.lang('root').'</a><ul>';
		}
		$where = (empty($except_id) ? '' : " AND id<>$except_id");
		$sql = "SELECT * from {$this->tbname} WHERE pid=$pid $where order by rank DESC,id ASC";
		$list = $this->db->sql_all($sql);
		if(isset($list['list']) && !empty($list['list'])){
			foreach($list['list'] as $row){
				if($sub == true && $row['hsub'] == 1){
					$sub_categorys = $this->get_category_tree_ul($row['id'], $sub, $except_id, $level + 1);
				}else{
					$sub_categorys = null;
				}
				if(!empty($sub_categorys)){
					$categorys[] = '<li><a href="#" onClick="set_category_id(this,'.$row['id'].','.$row['hsub'].');return false;">'.$row['name'].'</a>';
					
					$categorys[] = '<ul>';
					// $categorys[] = '<li class="topline"></li>';
					$categorys[] = $sub_categorys;
					$categorys[] = '</ul></li>';
				}else{
					$categorys[] = '<li><a href="#" onClick="set_category_id(this,'.$row['id'].','.$row['hsub'].');return false;">'.$row['name'].'</a></li>';
				}
				
			}
		}
		
		if($pid == 0){
			$categorys[] = '</ul></li>';
		}
		return implode("\n", $categorys);
	}
	
	public function get_category_tree($pid = 0, $sub = true, $except_id = 0, $level = 0, $path = ''){
		$categorys = array();
		$list = $this->db->get_all($this->tbname, '*', array('pid' => $pid), array('rank' => 'DESC'));
		if(isset($list['list']) && !empty($list['list'])){
			foreach($list['list'] as $row){
				$row['level'] = $level;
				$row['path'] = $path == '' ? $row['mark'] : $path.'/'.$row['mark'];
				if($sub == true && $row['hsub'] == 1){
					$sub_categorys = $this->get_category_tree($row['id'], $keyword, $except_id, $level + 1, $row['path']);
				}else{
					$sub_categorys = null;
				}
				if(!empty($sub_categorys)){
					$row['categorys'] = $sub_categorys;
				}else{
					if($sub == true && $row['hsub'] == 1)$this->db->update($this->tbname, array('id' => $row['id']), array('hsub' => 0));
				}
				$categorys[$row['id']] = $row;			
			}
		}
		return $categorys;
	}


	public static function get_child_categorys($catid){
		$child_category_arr = cache_read('news_child_category.php');
		if(empty($child_category_arr)){
			$child_category_arr = array();
		}
		if(!isset($child_category_arr[$catid])){
			$oNewscat = & Newscat::instance();
			$child_category_arr[$catid] = $oNewscat->get_category_list_children($childs, $catid);
			cache_write('news_child_category.php', $child_category_arr);
		}
		return $child_category_arr[$catid];
	}
	
	/********************************************************************************
	 * 缓存
	 ********************************************************************************/
	
	public function cache_news_category(){
		$category_list = $this->get_category_list();
		$category_tree = $this->get_category_tree();
		$category_mark_id_list = array();
		foreach($category_list as $category_id => $category_info){
			$category_mark_id_list[$category_info['mark']] = $category_id;
			if($category_info['hsub'] > 0){
				$category_info['subcats_list'] = $this->get_child_categorys($category_info['id']);
			}
			$category_list[$category_id] = $category_info;
		}
		$r = cache_write('news_categorys.php', array('category_list' => $category_list, 'category_tree' => $category_tree, 'category_mark_id_list' => $category_mark_id_list,));
		if($r > 0){
			return 1;
		}else{
			return -1;
		}
	}
}
?>